package com.quexin.pickmedialib;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.media.MediaScannerConnection;
import android.net.Uri;
import android.provider.MediaStore;
import android.text.TextUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.Locale;

public class MediaUtils {

    private static final String[] IMAGE_TYPE = new String[]{
            "image/jpeg", "image/png", "image/jpg", "image/bmp", "image/webp"};

    private static final String[] IMAGE_TYPE1 = new String[]{
            "image/jpeg", "image/png", "image/jpg", "image/bmp", "image/webp", "image/gif"};

    private static String getImageSelection(String[] imageType) {
        StringBuilder sb = new StringBuilder(MediaStore.Images.Media.MIME_TYPE);
        for (int i = 0; i < imageType.length - 1; i++) {
            sb.append("=? or ");
            sb.append(MediaStore.Images.Media.MIME_TYPE);
        }
        sb.append("=?");
        return sb.toString();
    }

    /**
     * 获取手机图片
     */
    public static void loadPictures(Activity context, LoadMediaCallback callback) {
        if (context == null || callback == null) {
            return;
        }
        new Thread(() -> {
            ArrayList<MediaModel> models = new ArrayList<>();
            Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
            String[] projection = {
                    MediaStore.Images.Media.DATA,
                    MediaStore.Images.Media.MIME_TYPE,
                    MediaStore.Images.Media.SIZE,
                    MediaStore.Images.Media.WIDTH,
                    MediaStore.Images.Media.HEIGHT
            };
            String sortOrder = MediaStore.Images.Media.DATE_MODIFIED + " desc";
            Cursor mCursor = context.getContentResolver().query(uri, projection,
                    getImageSelection(IMAGE_TYPE), IMAGE_TYPE, sortOrder);
            if (mCursor != null) {
                while (mCursor.moveToNext()) {
                    String path = mCursor.getString(mCursor.getColumnIndex(
                            MediaStore.Images.Media.DATA));
                    File file = new File(path);
                    if (!file.exists()) {
                        continue;
                    }
                    long size = mCursor.getLong(mCursor.getColumnIndex(
                            MediaStore.Images.Media.SIZE));
                    if (size <= 1024) {
                        continue;
                    }
                    MediaModel model = new MediaModel();
                    model.setPath(path);
                    model.setName(file.getName());
                    model.setSizeV(size);
                    model.setSize(FileUtils.convertFileSize(size));
                    model.setMimeType(mCursor.getString(mCursor.getColumnIndex(
                            MediaStore.Images.Media.MIME_TYPE)));
                    int w = mCursor.getInt(mCursor.getColumnIndex(
                            MediaStore.Images.Media.WIDTH));
                    int h = mCursor.getInt(mCursor.getColumnIndex(
                            MediaStore.Images.Media.HEIGHT));
                    if (w == 0 || h == 0) {
                        int[] imgSize = ImageUtils.getImageSize(file.getPath());
                        w = imgSize[0];
                        h = imgSize[1];
                    }
                    model.setWidth(w);
                    model.setHeight(h);
                    model.setFlag(0);
                    models.add(model);
                }
                mCursor.close();
                context.runOnUiThread(() -> callback.callback(models));
            }
        }).start();
    }

    /**
     * 获取手机图片(指定路径)
     */
    public static void loadPictures(
            Activity context, String appointPath, boolean loadGif, LoadMediaCallback callback) {
        if (context == null || callback == null) {
            return;
        }
        new Thread(() -> {
            ArrayList<MediaModel> models = new ArrayList<>();
            Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
            String[] projection = {
                    MediaStore.Images.Media.DATA,
                    MediaStore.Images.Media.MIME_TYPE,
                    MediaStore.Images.Media.SIZE,
                    MediaStore.Images.Media.WIDTH,
                    MediaStore.Images.Media.HEIGHT
            };
            String sortOrder = MediaStore.Images.Media.DATE_MODIFIED + " desc";
            Cursor mCursor;
            if (loadGif) {
                mCursor = context.getContentResolver().query(uri, projection,
                        getImageSelection(IMAGE_TYPE1), IMAGE_TYPE1, sortOrder);
            } else {
                mCursor = context.getContentResolver().query(uri, projection,
                        getImageSelection(IMAGE_TYPE), IMAGE_TYPE, sortOrder);
            }
            if (mCursor != null) {
                while (mCursor.moveToNext()) {
                    String path = mCursor.getString(mCursor.getColumnIndex(
                            MediaStore.Audio.Media.DATA));
                    File file = new File(path);
                    if (!file.exists()) {
                        continue;
                    }
                    long size = mCursor.getLong(mCursor.getColumnIndex(
                            MediaStore.Images.Media.SIZE));;
                    if (size <= 1024) {
                        continue;
                    }
                    if (file.getParentFile() == null) {
                        continue;
                    }
                    if (!file.getParentFile().getAbsolutePath().equals(appointPath)) {
                        continue;
                    }
                    MediaModel model = new MediaModel();
                    model.setPath(path);
                    model.setName(file.getName());
                    model.setSizeV(size);
                    model.setSize(FileUtils.convertFileSize(size));
                    model.setMimeType(mCursor.getString(mCursor.getColumnIndex(
                            MediaStore.Images.Media.MIME_TYPE)));
                    int w = mCursor.getInt(mCursor.getColumnIndex(
                            MediaStore.Images.Media.WIDTH));
                    int h = mCursor.getInt(mCursor.getColumnIndex(
                            MediaStore.Images.Media.HEIGHT));
                    if (w == 0 || h == 0) {
                        int[] imgSize = ImageUtils.getImageSize(file.getPath());
                        w = imgSize[0];
                        h = imgSize[1];
                    }
                    model.setWidth(w);
                    model.setHeight(h);
                    model.setFlag(0);
                    models.add(model);
                }
                mCursor.close();
                context.runOnUiThread(() -> callback.callback(models));
            }
        }).start();
    }

    /**
     * 获取手机第一张图片
     */
    public static void loadFirstPicture(Activity context, LoadFirstMediaCallback callback) {
        if (context == null || callback == null) {
            return;
        }
        new Thread(() -> {
            Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
            String[] projection = {
                    MediaStore.Images.Media.DATA,
                    MediaStore.Images.Media.MIME_TYPE,
                    MediaStore.Images.Media.SIZE,
                    MediaStore.Images.Media.WIDTH,
                    MediaStore.Images.Media.HEIGHT
            };
            String sortOrder = MediaStore.Images.Media.DATE_MODIFIED + " desc";
            Cursor mCursor = context.getContentResolver().query(uri, projection,
                    getImageSelection(IMAGE_TYPE), IMAGE_TYPE, sortOrder);
            if (mCursor != null) {
                MediaModel model = new MediaModel();
                while (mCursor.moveToNext()) {
                    String path = mCursor.getString(mCursor.getColumnIndex(
                            MediaStore.Images.Media.DATA));
                    File file = new File(path);
                    if (!file.exists()) {
                        continue;
                    }
                    long size = mCursor.getLong(mCursor.getColumnIndex(
                            MediaStore.Images.Media.SIZE));
                    if (size <= 1024) {
                        continue;
                    }
                    model.setPath(path);
                    model.setName(file.getName());
                    model.setSizeV(size);
                    model.setSize(FileUtils.convertFileSize(size));
                    model.setMimeType(mCursor.getString(mCursor.getColumnIndex(
                            MediaStore.Images.Media.MIME_TYPE)));
                    int w = mCursor.getInt(mCursor.getColumnIndex(
                            MediaStore.Images.Media.WIDTH));
                    int h = mCursor.getInt(mCursor.getColumnIndex(
                            MediaStore.Images.Media.HEIGHT));
                    if (w == 0 || h == 0) {
                        int[] imgSize = ImageUtils.getImageSize(file.getPath());
                        w = imgSize[0];
                        h = imgSize[1];
                    }
                    model.setWidth(w);
                    model.setHeight(h);
                    model.setFlag(0);
                    break;
                }
                mCursor.close();
                context.runOnUiThread(() -> callback.callback(model));
            }
        }).start();
    }

    /**
     * 获取手机图片
     * 只获取Gif
     */
    public static void loadPicturesGif(Activity context, LoadMediaCallback callback) {
        if (context == null || callback == null) {
            return;
        }
        new Thread(() -> {
            ArrayList<MediaModel> models = new ArrayList<>();
            Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
            String[] projection = {
                    MediaStore.Images.Media.DATA,
                    MediaStore.Images.Media.MIME_TYPE,
                    MediaStore.Images.Media.SIZE,
                    MediaStore.Images.Media.WIDTH,
                    MediaStore.Images.Media.HEIGHT
            };
            String sortOrder = MediaStore.Images.Media.DATE_MODIFIED + " desc";
            Cursor mCursor = context.getContentResolver().query(uri, projection,
                    MediaStore.Images.Media.MIME_TYPE + "=?",
                    new String[]{"image/gif"}, sortOrder);
            if (mCursor != null) {
                while (mCursor.moveToNext()) {
                    String path = mCursor.getString(mCursor.getColumnIndex(
                            MediaStore.Images.Media.DATA));
                    File file = new File(path);
                    if (!file.exists()) {
                        continue;
                    }
                    long size = mCursor.getLong(mCursor.getColumnIndex(
                            MediaStore.Images.Media.SIZE));
                    if (size <= 1024) {
                        continue;
                    }
                    MediaModel model = new MediaModel();
                    model.setPath(path);
                    model.setName(file.getName());
                    model.setSizeV(size);
                    model.setSize(FileUtils.convertFileSize(size));
                    model.setMimeType(mCursor.getString(mCursor.getColumnIndex(
                            MediaStore.Images.Media.MIME_TYPE)));
                    int w = mCursor.getInt(mCursor.getColumnIndex(
                            MediaStore.Images.Media.WIDTH));
                    int h = mCursor.getInt(mCursor.getColumnIndex(
                            MediaStore.Images.Media.HEIGHT));
                    if (w == 0 || h == 0) {
                        int[] imgSize = ImageUtils.getImageSize(file.getPath());
                        w = imgSize[0];
                        h = imgSize[1];
                    }
                    model.setWidth(w);
                    model.setHeight(h);
                    model.setFlag(0);
                    models.add(model);
                }
                mCursor.close();
                context.runOnUiThread(() -> callback.callback(models));
            }
        }).start();
    }

    /**
     * 获取手机图片(指定路径)
     * 只获取Gif
     */
    public static void loadPicturesGif(Activity context, String appointPath, LoadMediaCallback callback) {
        if (context == null || callback == null) {
            return;
        }
        new Thread(() -> {
            ArrayList<MediaModel> models = new ArrayList<>();
            try {
                File appointFile = new File(appointPath);
                if (appointFile.exists()) {
                    File[] files = appointFile.listFiles();
                    if (files != null && files.length > 0) {
                        for (File file : files) {
                            long size = FileUtils.getFileSize(file);
                            if (size <= 1024) {
                                continue;
                            }
                            if (file.getName().toUpperCase().endsWith(".GIF")) {
                                MediaModel model = new MediaModel();
                                model.setPath(file.getAbsolutePath());
                                model.setName(file.getName());
                                model.setSizeV(size);
                                model.setSize(FileUtils.convertFileSize(size));
                                model.setMimeType("image/gif");
                                int[] wAndH = ImageUtils.getImageSize(file.getPath());
                                model.setWidth(wAndH[0]);
                                model.setHeight(wAndH[1]);
                                model.setFlag(0);
                                models.add(model);
                            }
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            context.runOnUiThread(() -> callback.callback(models));
        }).start();
    }

    /**
     * 获取手机音频
     */
    public static void loadAudios(Activity context, LoadMediaCallback callback) {
        if (context == null || callback == null) {
            return;
        }
        new Thread(() -> {
            ArrayList<MediaModel> models = new ArrayList<>();
            Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
            String[] projection = {
                    MediaStore.Audio.Media.DATA,
                    MediaStore.Audio.Media.SIZE,
                    // MediaStore.Audio.Media.DURATION,
                    "duration",
                    MediaStore.Audio.Media.MIME_TYPE
            };
            String sortOrder = MediaStore.Audio.Media.DATE_MODIFIED + " desc";
            Cursor mCursor = context.getContentResolver().query(uri, projection,
                    "", new String[]{}, sortOrder);
            if (mCursor != null) {
                while (mCursor.moveToNext()) {
                    String path = mCursor.getString(mCursor.getColumnIndex(
                            MediaStore.Audio.Media.DATA));
                    File file = new File(path);
                    if (!file.exists()) {
                        continue;
                    }
                    long size = mCursor.getLong(mCursor.getColumnIndex(
                            MediaStore.Audio.Media.SIZE));
                    if (size <= 1024) {
                        continue;
                    }
                    long duration = mCursor.getLong(mCursor.getColumnIndex("duration"));
                    if (duration <= 0) {
                        continue;
                    }
                    String duration1 = MediaUtils.updateTime2(duration);
                    if (duration1.equals("00:00")) {
                        continue;
                    }
                    MediaModel model = new MediaModel();
                    model.setPath(path);
                    model.setName(file.getName());
                    model.setSizeV(size);
                    model.setSize(FileUtils.convertFileSize(size));
                    model.setDuration(duration, duration1);
                    model.setMimeType(mCursor.getString(mCursor.getColumnIndex(
                            MediaStore.Audio.Media.MIME_TYPE)));
                    model.setFlag(1);
                    models.add(model);
                }
                mCursor.close();
                context.runOnUiThread(() -> callback.callback(models));
            }
        }).start();
    }

    /**
     * 获取手机音频(指定路径)
     */
    public static void loadAudios(Activity context, String appointPath, LoadMediaCallback callback) {
        if (context == null || callback == null) {
            return;
        }
        new Thread(() -> {
            ArrayList<MediaModel> models = new ArrayList<>();
            Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
            String[] projection = {
                    MediaStore.Audio.Media.DATA,
                    MediaStore.Audio.Media.SIZE,
                    // MediaStore.Audio.Media.DURATION,
                    "duration",
                    MediaStore.Audio.Media.MIME_TYPE
            };
            String sortOrder = MediaStore.Audio.Media.DATE_MODIFIED + " desc";
            Cursor mCursor = context.getContentResolver().query(uri, projection,
                    "", new String[]{}, sortOrder);
            if (mCursor != null) {
                while (mCursor.moveToNext()) {
                    String path = mCursor.getString(mCursor.getColumnIndex(
                            MediaStore.Audio.Media.DATA));
                    File file = new File(path);
                    if (!file.exists()) {
                        continue;
                    }
                    long size = mCursor.getLong(mCursor.getColumnIndex(
                            MediaStore.Audio.Media.SIZE));
                    if (size <= 1024) {
                        continue;
                    }
                    if (file.getParentFile() == null) {
                        continue;
                    }
                    if (!file.getParentFile().getAbsolutePath().equals(appointPath)) {
                        continue;
                    }
                    long duration = mCursor.getLong(mCursor.getColumnIndex("duration"));
                    if (duration <= 0) {
                        continue;
                    }
                    String duration1 = MediaUtils.updateTime2(duration);
                    if (duration1.equals("00:00")) {
                        continue;
                    }
                    MediaModel model = new MediaModel();
                    model.setPath(path);
                    model.setName(file.getName());
                    model.setSizeV(size);
                    model.setSize(FileUtils.convertFileSize(size));
                    model.setDuration(duration, duration1);
                    model.setMimeType(mCursor.getString(mCursor.getColumnIndex(
                            MediaStore.Audio.Media.MIME_TYPE)));
                    model.setFlag(1);
                    models.add(model);
                }
                mCursor.close();
                context.runOnUiThread(() -> callback.callback(models));
            }
        }).start();
    }

    /**
     * 获取手机视频
     */
    public static void loadVideos(Activity context, LoadMediaCallback callback) {
        if (context == null || callback == null) {
            return;
        }
        new Thread(() -> {
            ArrayList<MediaModel> models = new ArrayList<>();
            Uri uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
            String[] projection = {
                    MediaStore.Video.Media.DATA,
                    // MediaStore.Video.Media.DURATION,
                    "duration",
                    MediaStore.Video.Media.MIME_TYPE,
                    MediaStore.Video.Media.SIZE,
                    MediaStore.Video.Media.WIDTH,
                    MediaStore.Video.Media.HEIGHT

            };
            String sortOrder = MediaStore.Video.Media.DATE_MODIFIED + " desc";
            Cursor mCursor = context.getContentResolver().query(uri, projection,
                    "", new String[]{}, sortOrder);
            if (mCursor != null) {
                while (mCursor.moveToNext()) {
                    String path = mCursor.getString(mCursor.getColumnIndex(
                            MediaStore.Video.Media.DATA));
                    File file = new File(path);
                    if (!file.exists()) {
                        continue;
                    }
                    long size = mCursor.getLong(mCursor.getColumnIndex(
                            MediaStore.Video.Media.SIZE));
                    if (size <= 1024) {
                        continue;
                    }
                    long duration = mCursor.getLong(mCursor.getColumnIndex("duration"));
                    if (duration <= 0) {
                        continue;
                    }
                    String duration1 = MediaUtils.updateTime2(duration);
                    if (duration1.equals("00:00")) {
                        continue;
                    }
                    MediaModel model = new MediaModel();
                    model.setPath(path);
                    model.setName(file.getName());
                    model.setSizeV(size);
                    model.setSize(FileUtils.convertFileSize(size));
                    model.setDuration(duration, duration1);
                    model.setMimeType(mCursor.getString(mCursor.getColumnIndex(
                            MediaStore.Video.Media.MIME_TYPE)));
                    model.setWidth(mCursor.getInt(mCursor.getColumnIndex(
                            MediaStore.Video.Media.WIDTH)));
                    model.setHeight(mCursor.getInt(mCursor.getColumnIndex(
                            MediaStore.Video.Media.HEIGHT)));
                    model.setFlag(2);
                    models.add(model);
                }
                mCursor.close();
                context.runOnUiThread(() -> callback.callback(models));
            }
        }).start();
    }

    /**
     * 获取手机视频(指定路径)
     */
    public static void loadVideos(Activity context, String appointPath, LoadMediaCallback callback) {
        if (context == null || callback == null) {
            return;
        }
        new Thread(() -> {
            ArrayList<MediaModel> models = new ArrayList<>();
            Uri uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
            String[] projection = {
                    MediaStore.Video.Media.DATA,
                    // MediaStore.Video.Media.DURATION,
                    "duration",
                    MediaStore.Video.Media.MIME_TYPE,
                    MediaStore.Video.Media.SIZE,
                    MediaStore.Video.Media.WIDTH,
                    MediaStore.Video.Media.HEIGHT

            };
            String sortOrder = MediaStore.Video.Media.DATE_MODIFIED + " desc";
            Cursor mCursor = context.getContentResolver().query(uri, projection,
                    "", new String[]{}, sortOrder);
            if (mCursor != null) {
                while (mCursor.moveToNext()) {
                    String path = mCursor.getString(mCursor.getColumnIndex(
                            MediaStore.Video.Media.DATA));
                    File file = new File(path);
                    if (!file.exists()) {
                        continue;
                    }
                    long size = mCursor.getLong(mCursor.getColumnIndex(
                            MediaStore.Video.Media.SIZE));
                    if (size <= 1024) {
                        continue;
                    }
                    if (file.getParentFile() == null) {
                        continue;
                    }
                    if (!file.getParentFile().getAbsolutePath().equals(appointPath)) {
                        continue;
                    }
                    long duration = mCursor.getLong(mCursor.getColumnIndex("duration"));
                    if (duration <= 0) {
                        continue;
                    }
                    String duration1 = MediaUtils.updateTime2(duration);
                    if (duration1.equals("00:00")) {
                        continue;
                    }
                    MediaModel model = new MediaModel();
                    model.setPath(path);
                    model.setName(file.getName());
                    model.setSizeV(size);
                    model.setSize(FileUtils.convertFileSize(size));
                    model.setDuration(duration, duration1);
                    model.setMimeType(mCursor.getString(mCursor.getColumnIndex(
                            MediaStore.Video.Media.MIME_TYPE)));
                    model.setWidth(mCursor.getInt(mCursor.getColumnIndex(
                            MediaStore.Video.Media.WIDTH)));
                    model.setHeight(mCursor.getInt(mCursor.getColumnIndex(
                            MediaStore.Video.Media.HEIGHT)));
                    model.setFlag(2);
                    models.add(model);
                }
                mCursor.close();
                context.runOnUiThread(() -> callback.callback(models));
            }
        }).start();
    }

    public interface LoadMediaCallback {
        void callback(ArrayList<MediaModel> models);
    }

    public interface LoadFirstMediaCallback {
        void callback(MediaModel model);
    }

    /**
     * 删除图片
     */
    public static void deletePicture(Context context, String filePath) {
        if (TextUtils.isEmpty(filePath)) {
            return;
        }
        Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
        ContentResolver mContentResolver = context.getContentResolver();
        String where = MediaStore.Images.Media.DATA + "='" + filePath + "'";
        mContentResolver.delete(uri, where, null);
    }

    /**
     * 删除视频
     */
    public static void deleteVideo(Context context, String filePath) {
        if (TextUtils.isEmpty(filePath)) {
            return;
        }
        Uri uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
        ContentResolver mContentResolver = context.getContentResolver();
        String where = MediaStore.Video.Media.DATA + "='" + filePath + "'";
        mContentResolver.delete(uri, where, null);
    }

    /**
     * 删除音频
     */
    public static void deleteAudio(Context context, String filePath) {
        if (TextUtils.isEmpty(filePath)) {
            return;
        }
        Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
        ContentResolver mContentResolver = context.getContentResolver();
        String where = MediaStore.Audio.Media.DATA + "='" + filePath + "'";
        mContentResolver.delete(uri, where, null);
    }

    /**
     * 修改音频名称
     */
    public static void updateAudioName(Context context, String oldPath, String newPath) {
        if (TextUtils.isEmpty(oldPath) || TextUtils.isEmpty(newPath)) {
            return;
        }
        try {
            Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
            ContentResolver mContentResolver = context.getContentResolver();
            String where = MediaStore.Audio.Media.DATA + "='" + oldPath + "'";
            ContentValues contentValues = new ContentValues();
            contentValues.put(MediaStore.Audio.Media.DATA, newPath);
            mContentResolver.update(uri, contentValues, where, null);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 修改视频名称
     */
    public static void updateVideoName(Context context, String oldPath, String newPath) {
        if (TextUtils.isEmpty(oldPath) || TextUtils.isEmpty(newPath)) {
            return;
        }
        try {
            Uri uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
            ContentResolver mContentResolver = context.getContentResolver();
            String where = MediaStore.Video.Media.DATA + "='" + oldPath + "'";
            ContentValues contentValues = new ContentValues();
            contentValues.put(MediaStore.Video.Media.DATA, newPath);
            mContentResolver.update(uri, contentValues, where, null);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 刷新系统媒体库
     */
    public static void refreshSystemMedia(Context context, String filePath) {
        if (TextUtils.isEmpty(filePath)) {
            return;
        }
        MediaScannerConnection.scanFile(context, new String[]{filePath}, null, (path, uri1) -> {
            Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
            mediaScanIntent.setData(uri1);
            context.sendBroadcast(mediaScanIntent);
        });
    }

    public static String updateTime2(long time) {
        long m = time / 1000 / 60;
        long s = time / 1000 % 60;
        return String.format(Locale.CHINA, "%02d:%02d", m, s);
    }

    public static String updateTime3(long time) {
        long h = time / 1000 / 60 / 60;
        long m = time / 1000 / 60 % 60;
        long s = time / 1000 % 60;
        return String.format(Locale.CHINA, "%02d:%02d:%02d", h, m, s);
    }

    public static String formatTimeUnit2(String string, long end, long start) {
        return string + updateTime2(end - start);
    }

    public static String formatTimeUnit3(String string, long end, long start) {
        return string + updateTime3(end - start);
    }

    public static String calcTime3(String startTime, String endTime) {
        try {
            String[] startTimes = startTime.split(":");
            String[] endTimes = endTime.split(":");
            int h = Integer.parseInt(endTimes[0]) - Integer.parseInt(startTimes[0]);
            int m = Integer.parseInt(endTimes[1]) - Integer.parseInt(startTimes[1]);
            int s = Integer.parseInt(endTimes[2]) - Integer.parseInt(startTimes[2]);
            return String.format(Locale.CHINA, "%02d:%02d:%02d", h, m, s);
        } catch (Exception e) {
            e.printStackTrace();
            return "00:00:00";
        }
    }
    public static String formatTimeUnit1(String string, int end, int start) {
        try {
            return String.format(
                    Locale.CHINA, "%s%02d:%02d:%02d", string,
                    (end - start) / 3600, ((end - start) % 3600) / 60, (end - start) % 60);
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }
}
